package org.testis.testdriver.parser;

import org.testis.data.TestStepResultType;
import org.testis.orm.conditionhandler.ConditionHandlerFactory;
import org.testis.testdriver.ITestBuilder;
import org.testis.testdriver.TestPlanBuilder;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.DataObjectRefined;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.TestDesign;

/**
 *
 * @author barisergun
 */
public class TestDriverDataObjectRefinedParsing extends TestDriverAbstractParsingStrategy {

    public TestDriverDataObjectRefinedParsing(ITestBuilder builder,
            TestDesignTreeNode currentNode) {
        m_builder = builder;
        m_currentNode = currentNode;

        if (m_currentNode.isClone()) {
            m_cloneString = " [clone]";
        }
    }

    @Override
    public void parseCurrentNode() throws PersistentException {
        DataObjectRefined testPart =
                (DataObjectRefined) m_currentNode.getObject();

        LinkedList<String> condList = getConditionsOfTestPart();

        LinkedHashMap<String, String> mapProcessed =
                new LinkedHashMap<String, String>();

        try {

            mapProcessed.putAll(testPart.getKeyValueHashMap());
            processMapStaticVariables(mapProcessed);
        } catch (RuntimeException eee) {
            if (m_builder instanceof TestPlanBuilder) {
                ((TestPlanBuilder) m_builder).pushExceptionMessage(
                        eee.getMessage()
                        + "Problem Node:"
                        + getCurrentNodePath());
            }

        }
        Integer expectedResultId = m_currentNode.getReference().getParameterIntValue(TestDesignTreeParameters.ExpectedResultTypeId.name());
        String expectedResultType = TestDesign.getExpectedResultTypeName(expectedResultId);
        m_builder.attachDataToStep(String.valueOf(m_currentNode.getId()),
                testPart.getName() + m_cloneString,
                TestStepResultType.valueOf(expectedResultType),
                mapProcessed,
                condList,
                String.valueOf(m_currentNode.getParent().
                getId()));

    }
}
